package com.vancouver.knows.ui.activity.set;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import butterknife.Bind;
import butterknife.ButterKnife;

import com.vancouver.knows.MainApplication;
import com.vancouver.knows.R;
import com.vancouver.knows.ui.common.SlipButtonView;
import com.vancouver.knows.ui.common.SlipButtonView.OnChangedListener;
import com.vancouver.knows.business.BSharedPrefManager;

public class MessageListAdapter extends BaseAdapter {
	private Context mContext;
	private String[] mAttrTitles;
	private LayoutInflater mInflater;
	private BSharedPrefManager sharePrefManager;

	public MessageListAdapter(Context context) {
		mContext = context;
		mInflater = LayoutInflater.from(context);
		sharePrefManager = ((MainApplication) context.getApplicationContext()).getSharedPrefManager();
		mAttrTitles = context.getResources().getStringArray(R.array.message_titles);
	}

	@Override
	public int getCount() {
		return mAttrTitles.length;
	}

	@Override
	public String getItem(int position) {
		return mAttrTitles[position];
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		ViewValueHand hand = null;
		View view = convertView;
		if (convertView == null) {
			view = mInflater.inflate(R.layout.message_remind_view, parent, false);
			hand = new ViewValueHand(view);

			view.setTag(hand);
		} else {
			hand = (ViewValueHand) view.getTag();
		}

		hand.title.setText(mAttrTitles[position]);
		switch (position) {
		case 0:
			hand.splitBtn.setCheck(sharePrefManager.getCloseAllMessage());
			hand.title.setText(sharePrefManager.getCloseAllMessage() ? mContext.getString(R.string.open_all) : mContext
					.getString(R.string.close_all));
			hand.splitBtn.setEnabled(true);
			if (!sharePrefManager.getCloseAllMessage()) {
				isSign = true;
			} else {
				isSign = false;
			}
			break;
		case 1:
			hand.splitBtn.setCheck(sharePrefManager.getMyArticleNewComment());
			if (isSign) {
				hand.splitBtn.setEnabled(false);
			} else {
				hand.splitBtn.setEnabled(true);
			}
			break;
		case 2:
			hand.splitBtn.setCheck(sharePrefManager.getMyFollowedBusiness());
			if (isSign) {
				hand.splitBtn.setEnabled(false);
			} else {
				hand.splitBtn.setEnabled(true);
			}
			break;
		case 3:
			hand.splitBtn.setCheck(sharePrefManager.getHaveAtCommentMessage());
			if (isSign) {
				hand.splitBtn.setEnabled(false);
			} else {
				hand.splitBtn.setEnabled(true);
			}
			break;
		}

		hand.splitBtn.SetOnChangedListener(new OnChangedListener() {

			public void OnChanged(boolean CheckState) {
				saveSplitStatusToShareManage(position, CheckState);
				if (position == 0) {
					if (sharePrefManager.getCloseAllMessage()) {
						sharePrefManager.saveMyArticleNewComment(true);
						sharePrefManager.saveMyFollowedBusiness(true);
						sharePrefManager.saveHaveAtCommentMessage(true);
					} else {
						sharePrefManager.saveMyArticleNewComment(false);
						sharePrefManager.saveMyFollowedBusiness(false);
						sharePrefManager.saveHaveAtCommentMessage(false);
					}
					notifyDataSetChanged();
				}
			}
		});

		return view;
	}

	private boolean isSign = false;

	public class ViewValueHand {
		@Bind(R.id.split_button)
		SlipButtonView splitBtn;
		@Bind(R.id.remind_title)
		TextView title;

		public ViewValueHand(View view) {
			ButterKnife.bind(this, view);
		}
	}

	private void saveSplitStatusToShareManage(int position, boolean CheckState) {
		switch (position) {
		case 0:
			sharePrefManager.saveCloseAllMessage(CheckState);
			break;
		case 1:
			sharePrefManager.saveMyArticleNewComment(CheckState);
			break;
		case 2:
			sharePrefManager.saveMyFollowedBusiness(CheckState);
			break;
		case 3:
			sharePrefManager.saveHaveAtCommentMessage(CheckState);
			break;

		default:
			break;
		}
	}

}
